In [1]:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
sns.set()
import plotly.express as px
import pymc3
import plotly.graph_objects as go
import plotly.figure_factory as ff
In [2]:
size=20000
#facebook
facebook_budget=15*4
conversion=[21,57]
reach=[554,1600]

#ecommerce
ali_price=12.35
shipping=0
selling_price=(ali_price+shipping)+(ali_price+shipping)*1.4

#payment_gate
two_checkout=(selling_price+shipping)*0.035+0.35
stripe=(selling_price+shipping)*0.029+0.3

#arrays
prior_arr=np.empty(size)
likelihood_arr=np.empty(size)
ecom_revenu_arr=np.empty(size)
ali_cost_arr=np.empty(size)
two_checkout_cost_arr=np.empty(size)
stripe_cost_arr=np.empty(size)
profit_two_arr=np.empty(size)
profit_stripe_arr=np.empty(size)
    
for i in range(size):
    
    prior=np.random.beta(np.random.randint(conversion[0],conversion[1]),(np.random.randint(reach[0],reach[1])-np.random.randint(conversion[0],conversion[1])))
    likelihood=np.random.binomial(np.mean(reach),prior)
        
    ecom_revenu=selling_price*likelihood
    ali_cost=(ali_price+shipping)*likelihood
    two_checkout_cost=two_checkout*likelihood
    stripe_cost=stripe*likelihood
    
    profit_two=ecom_revenu-ali_cost-two_checkout_cost-facebook_budget
    profit_stripe=ecom_revenu-ali_cost-stripe_cost-facebook_budget
    
    prior_arr[i]=round(prior,3)
    likelihood_arr[i]=likelihood
    ecom_revenu_arr[i]=round(ecom_revenu,3)
    ali_cost_arr[i]=round(ali_cost,3)
    two_checkout_cost_arr[i]=round(two_checkout_cost,3)
    stripe_cost_arr[i]=round(stripe_cost,3)
    profit_two_arr[i]=round(profit_two,3)
    profit_stripe_arr[i]=round(profit_stripe,3)
df=pd.DataFrame(data={"prior_arr":prior_arr,"likelihood_arr":likelihood_arr,"ecom_revenu_arr":ecom_revenu_arr,
                   "ali_cost_arr":ali_cost_arr,"two_checkout_cost_arr":two_checkout_cost_arr,"stripe_cost_arr":stripe_cost_arr,
                   "profit_two_arr":profit_two_arr,"profit_stripe_arr":profit_stripe_arr})
df["profit_per_unit"]=round(df["profit_two_arr"]/df["likelihood_arr"],3)
print("Selling price is:",selling_price)
for i in ["profit_two_arr","profit_per_unit","prior_arr","likelihood_arr","ali_cost_arr"]:
    fig1=px.histogram(df,x=i,marginal='box')

    fig1.add_trace(
        go.Scatter(
            x=[pymc3.stats.hpd(df[i])[0], pymc3.stats.hpd(df[i])[0]],
            y=[0, 200],
            mode="lines",
            line=go.scatter.Line(color="red"),
            showlegend=False))
    fig1.add_trace(
        go.Scatter(
            x=[pymc3.stats.hpd(df[i])[1], pymc3.stats.hpd(df[i])[1]],
            y=[0, 200],
            mode="lines",
            line=go.scatter.Line(color="red"),
            showlegend=False))
    if i =="prior_arr":
        print("The HPD of the "+i+" is bettwen",round(pymc3.stats.hpd(df[i])[0]*100,2),"%","and",round(pymc3.stats.hpd(df[i])[1]*100,2),"%")
    else:
        print("The HPD of the "+i+" is bettwen",pymc3.stats.hpd(df[i])[0],"and",pymc3.stats.hpd(df[i])[1])
    fig1.show()
    print("________________________________________________")
Selling price is: 29.64
The HPD of the profit_two_arr is bettwen 99.026 and 1196.305
________________________________________________
The HPD of the profit_per_unit is bettwen 12.373 and 15.303
________________________________________________
The HPD of the prior_arr is bettwen 1.1 % and 7.1 %
________________________________________________
The HPD of the likelihood_arr is bettwen 9.0 and 78.0
________________________________________________
The HPD of the ali_cost_arr is bettwen 111.15 and 963.3
________________________________________________
In [3]:
#worst case
size=20000
#facebook
facebook_budget=15*4
conversion=[21]
reach=[1600]

#ecommerce
ali_price=12.35
shipping=0
selling_price=(ali_price+shipping)+(ali_price+shipping)*1.4

#payment_gate
two_checkout=(selling_price+shipping)*0.035+0.35
stripe=(selling_price+shipping)*0.029+0.3

#arrays
prior_arr=np.empty(size)
likelihood_arr=np.empty(size)
ecom_revenu_arr=np.empty(size)
ali_cost_arr=np.empty(size)
two_checkout_cost_arr=np.empty(size)
stripe_cost_arr=np.empty(size)
profit_two_arr=np.empty(size)
profit_stripe_arr=np.empty(size)
    
for i in range(size):
    
    prior=np.random.beta(np.mean(conversion),(np.mean(reach)-np.mean(conversion)))
    likelihood=np.random.binomial(np.mean(reach),prior)
        
    ecom_revenu=selling_price*likelihood
    ali_cost=(ali_price+shipping)*likelihood
    two_checkout_cost=two_checkout*likelihood
    stripe_cost=stripe*likelihood
    
    profit_two=ecom_revenu-ali_cost-two_checkout_cost-facebook_budget
    profit_stripe=ecom_revenu-ali_cost-stripe_cost-facebook_budget
    
    prior_arr[i]=round(prior,3)
    likelihood_arr[i]=likelihood
    ecom_revenu_arr[i]=round(ecom_revenu,3)
    ali_cost_arr[i]=round(ali_cost,3)
    two_checkout_cost_arr[i]=round(two_checkout_cost,3)
    stripe_cost_arr[i]=round(stripe_cost,3)
    profit_two_arr[i]=round(profit_two,3)
    profit_stripe_arr[i]=round(profit_stripe,3)
df=pd.DataFrame(data={"prior_arr":prior_arr,"likelihood_arr":likelihood_arr,"ecom_revenu_arr":ecom_revenu_arr,
                   "ali_cost_arr":ali_cost_arr,"two_checkout_cost_arr":two_checkout_cost_arr,"stripe_cost_arr":stripe_cost_arr,
                   "profit_two_arr":profit_two_arr,"profit_stripe_arr":profit_stripe_arr})
df["profit_per_unit"]=round(df["profit_two_arr"]/df["likelihood_arr"],3)
print("Selling price is:",selling_price)
for i in ["profit_two_arr","profit_per_unit","prior_arr","likelihood_arr","ali_cost_arr"]:
    fig1=px.histogram(df,x=i,marginal='box')

    fig1.add_trace(
        go.Scatter(
            x=[pymc3.stats.hpd(df[i])[0], pymc3.stats.hpd(df[i])[0]],
            y=[0, 200],
            mode="lines",
            line=go.scatter.Line(color="red"),
            showlegend=False))
    fig1.add_trace(
        go.Scatter(
            x=[pymc3.stats.hpd(df[i])[1], pymc3.stats.hpd(df[i])[1]],
            y=[0, 200],
            mode="lines",
            line=go.scatter.Line(color="red"),
            showlegend=False))
    if i =="prior_arr":
        print("The HPD of the "+i+" is bettwen",round(pymc3.stats.hpd(df[i])[0]*100,2),"%","and",round(pymc3.stats.hpd(df[i])[1]*100,2),"%")
    else:
        print("The HPD of the "+i+" is bettwen",pymc3.stats.hpd(df[i])[0],"and",pymc3.stats.hpd(df[i])[1])
    fig1.show()
    print("________________________________________________")
Selling price is: 29.64
The HPD of the profit_two_arr is bettwen 83.123 and 448.883
________________________________________________
The HPD of the profit_per_unit is bettwen 10.903 and 14.364
________________________________________________
The HPD of the prior_arr is bettwen 0.8 % and 1.8 %
________________________________________________
The HPD of the likelihood_arr is bettwen 9.0 and 32.0
________________________________________________
The HPD of the ali_cost_arr is bettwen 111.15 and 395.2
________________________________________________
In [4]:
#worst case
size=10000
#facebook
facebook_budget=15*4
conversion=[21]
reach=[1600]

#ecommerce
ali_price=12.35
shipping=0
selling_price=(ali_price+shipping)+(ali_price+shipping)*0.5

#payment_gate
two_checkout=(selling_price+shipping)*0.035+0.35
stripe=(selling_price+shipping)*0.029+0.3

#arrays
prior_arr=np.empty(size)
likelihood_arr=np.empty(size)
ecom_revenu_arr=np.empty(size)
ali_cost_arr=np.empty(size)
two_checkout_cost_arr=np.empty(size)
stripe_cost_arr=np.empty(size)
profit_two_arr=np.empty(size)
profit_stripe_arr=np.empty(size)
    
for i in range(size):
    
    prior=np.random.beta(np.mean(conversion),(np.mean(reach)-np.mean(conversion)))
    likelihood=np.random.binomial(np.mean(reach),prior)
        
    ecom_revenu=selling_price*likelihood
    ali_cost=(ali_price+shipping)*likelihood
    two_checkout_cost=two_checkout*likelihood
    stripe_cost=stripe*likelihood
    
    profit_two=ecom_revenu-ali_cost-two_checkout_cost-facebook_budget
    profit_stripe=ecom_revenu-ali_cost-stripe_cost-facebook_budget
    
    prior_arr[i]=round(prior,3)
    likelihood_arr[i]=likelihood
    ecom_revenu_arr[i]=round(ecom_revenu,3)
    ali_cost_arr[i]=round(ali_cost,3)
    two_checkout_cost_arr[i]=round(two_checkout_cost,3)
    stripe_cost_arr[i]=round(stripe_cost,3)
    profit_two_arr[i]=round(profit_two,3)
    profit_stripe_arr[i]=round(profit_stripe,3)
df=pd.DataFrame(data={"prior_arr":prior_arr,"likelihood_arr":likelihood_arr,"ecom_revenu_arr":ecom_revenu_arr,
                   "ali_cost_arr":ali_cost_arr,"two_checkout_cost_arr":two_checkout_cost_arr,"stripe_cost_arr":stripe_cost_arr,
                   "profit_two_arr":profit_two_arr,"profit_stripe_arr":profit_stripe_arr})
df["profit_per_unit"]=round(df["profit_two_arr"]/df["likelihood_arr"],3)
print("Selling price is:",selling_price)
for i in ["profit_two_arr","profit_per_unit","prior_arr","likelihood_arr","ali_cost_arr"]:
    fig1=px.histogram(df,x=i,marginal='box',)

    fig1.add_trace(
        go.Scatter(
            x=[pymc3.stats.hpd(df[i])[0], pymc3.stats.hpd(df[i])[0]],
            y=[0, 200],
            mode="lines",
            line=go.scatter.Line(color="red"),
            showlegend=False))
    fig1.add_trace(
        go.Scatter(
            x=[pymc3.stats.hpd(df[i])[1], pymc3.stats.hpd(df[i])[1]],
            y=[0, 200],
            mode="lines",
            line=go.scatter.Line(color="red"),
            showlegend=False))
    if i =="prior_arr":
        print("The HPD of the "+i+" is bettwen",round(pymc3.stats.hpd(df[i])[0]*100,2),"%","and",round(pymc3.stats.hpd(df[i])[1]*100,2),"%")
    else:
        print("The HPD of the "+i+" is bettwen",pymc3.stats.hpd(df[i])[0],"and",pymc3.stats.hpd(df[i])[1])
    fig1.show()
    print("________________________________________________")
Selling price is: 18.525
The HPD of the profit_two_arr is bettwen -8.234 and 110.829
________________________________________________
The HPD of the profit_per_unit is bettwen 0.177 and 3.638
________________________________________________
The HPD of the prior_arr is bettwen 0.8 % and 1.8 %
________________________________________________
The HPD of the likelihood_arr is bettwen 10.0 and 33.0
________________________________________________
The HPD of the ali_cost_arr is bettwen 123.5 and 407.55
________________________________________________
In [5]:
for i in ["profit_two_arr","profit_per_unit","prior_arr","likelihood_arr","ali_cost_arr"]:
    fig1=px.histogram(df,x=i,marginal='box',histnorm="probability")

    fig1.add_trace(
        go.Scatter(
            x=[pymc3.stats.hpd(df[i])[0], pymc3.stats.hpd(df[i])[0]],
            y=[0, 0.1],
            mode="lines",
            line=go.scatter.Line(color="red"),
            showlegend=False))
    fig1.add_trace(
        go.Scatter(
            x=[pymc3.stats.hpd(df[i])[1], pymc3.stats.hpd(df[i])[1]],
            y=[0, 0.1],
            mode="lines",
            line=go.scatter.Line(color="red"),
            showlegend=False))
    if i =="prior_arr":
        print("The HPD of the "+i+" is bettwen",round(pymc3.stats.hpd(df[i])[0]*100,2),"%","and",round(pymc3.stats.hpd(df[i])[1]*100,2),"%")
    else:
        print("The HPD of the "+i+" is bettwen",pymc3.stats.hpd(df[i])[0],"and",pymc3.stats.hpd(df[i])[1])
    fig1.show()
    print("________________________________________________")
The HPD of the profit_two_arr is bettwen -8.234 and 110.829
________________________________________________
The HPD of the profit_per_unit is bettwen 0.177 and 3.638
________________________________________________
The HPD of the prior_arr is bettwen 0.8 % and 1.8 %
________________________________________________
The HPD of the likelihood_arr is bettwen 10.0 and 33.0
________________________________________________
The HPD of the ali_cost_arr is bettwen 123.5 and 407.55
________________________________________________